package handler

import (
	"api/pkg"
	__ "api/proto"
	"api/request"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"net/http"
)

func EatAdd(c *gin.Context) {
	var req request.EatAdd
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "验证失败",
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewEatClient(conn)
	add, err := c1.EatAdd(c, &__.EatAddRequest{
		Name:  req.Name,
		Num:   int64(req.Num),
		Price: float32(req.Price),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"data": add,
		"msg":  "添加成功",
	})
	return
}

func EatUpdate(c *gin.Context) {
	var req request.EatUpdate
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "验证失败",
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewEatClient(conn)
	update, err := c1.EatUpdate(c, &__.EatUpdateRequest{
		Id:  int64(req.Id),
		Num: int64(req.Num),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "修改失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"data": update,
		"msg":  "修改成功",
	})
	return
}

func Register(c *gin.Context) {
	var req request.Register
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "验证失败",
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewEatClient(conn)
	register, err := c1.Register(c, &__.RegisterRequest{
		Tel:      req.Tel,
		Password: req.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "注册失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"data": register,
		"msg":  "注册成功",
	})
	return
}

func Login(c *gin.Context) {
	var req request.Login
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "验证失败",
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewEatClient(conn)
	login, err := c1.Login(c, &__.LoginRequest{
		Tel:      req.Tel,
		Password: req.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "登录失败",
		})
		return
	}
	token, err := pkg.NewJWT("2210a").CreateToken(pkg.CustomClaims{
		ID: uint(login.Id)})

	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"data": token,
		"msg":  "登录成功",
	})
	return
}

func List(c *gin.Context) {
	var req request.List
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "验证失败",
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewEatClient(conn)
	xx, err := c1.GoodList(c, &__.GoodListRequest{})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "展示失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"data": xx,
		"msg":  "展示成功",
	})
	return
}
